/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.employee.service;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.mapper.JsonMapper;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.base.dao.StoreRoleDao;
import com.chlitina.store.modules.employee.dao.EmployeeDao;
import com.chlitina.store.modules.employee.entity.Employee;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

/**
 * 店员Service
 * @author lvbo
 * @version 2013-07-05
 */
@Component
@Transactional(readOnly = true)
public class EmployeeService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(EmployeeService.class);
	
	@Autowired
	private StoreRoleDao storeRoleDao;
	
	@Autowired
	private EmployeeDao employeeDao;
	
	public Employee get(Long id) {
		return employeeDao.findOne(id);
	}
	
	public Page<Employee> find(Page<Employee> page, Employee employee, int status) {
		page.setPageSize(Integer.parseInt(Global.getConfig("page.longPageSize")));
		DetachedCriteria dc = employeeDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store", StoreUtils.getStore()));
		if (StringUtils.isNotEmpty(employee.getName())){
			dc.add(Restrictions.like("name", "%"+employee.getName()+"%"));
		}
		dc.add(Restrictions.eq("status", status));
		dc.addOrder(Order.desc("id"));
		return employeeDao.find(page, dc);
	}
	
	public List<Employee> export(String name, int status) {
		employeeDao.clear();
		DetachedCriteria dc = employeeDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store", StoreUtils.getStore()));
		if (StringUtils.isNotEmpty(name)){
			dc.add(Restrictions.like("name", "%"+name+"%"));
		}
		dc.add(Restrictions.eq("status", status));
		dc.addOrder(Order.desc("id"));
		return employeeDao.find(dc);
	}
	
	public List<Employee> findByStore(Store store, int status) {
		DetachedCriteria dc = employeeDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store", store));
		dc.add(Restrictions.eq("status", status));
		return employeeDao.find(dc);
	}
	
	@Transactional(readOnly = false)
	public void save(Employee employee) {
		employee.setStoreRole(storeRoleDao.findOne(employee.getStoreRole().getId()));
		employeeDao.clear();
		employeeDao.save(employee);
//		employeeDao.refresh(employee);
	}
	
	@Transactional(readOnly = false)
	public void changeStatus(Employee employee) {
		int status = employee.getStatus();
		if (status == Employee.STATUS_WORKING) {
			employee.setStatus(Employee.STATUS_NONWORKING);
		}
		if (status == Employee.STATUS_NONWORKING) {
			employee.setStatus(Employee.STATUS_WORKING);
		}
		employeeDao.clear();
		employeeDao.save(employee);
		employeeDao.refresh(employee);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		employeeDao.delete(id);
	}
	
	@Transactional(readOnly = false)
	public List<Employee> isExist(String sfzh) {
		DetachedCriteria dc = employeeDao.createDetachedCriteria();
		dc.add(Restrictions.eq("sfzh", sfzh));
		return employeeDao.find(dc);
	}
	
	
	public String getEmployees(){
		List<Employee> employeeList = findByStore(StoreUtils.getStore(), Employee.STATUS_WORKING);
		Set<Map<String, Object>> set = Sets.newHashSet();
		for (Employee smployee : employeeList) {
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", smployee.getId());
			map.put("name", smployee.getName());
			map.put("mobile", smployee.getMobile());
			set.add(map);
		}
		System.err.println(JsonMapper.getInstance().toJson(set));
		return JsonMapper.getInstance().toJson(set);
	}
	
}
